package cn.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Function;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

public class test2 {

	public static void main(String[] args)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		// SFunction<SysUser, ?> function = propertySFunction(SysUser.class,
		// "userId");
		// System.out.println(function);
		// LambdaMeta extract = LambdaUtils.extract(function);
		// System.out.println(extract.getImplMethodName());
		//
		// SFunction<SysUser, ?> function1 = SysUser::getUserId;
		// System.out.println(function1);
		// LambdaMeta extract1 = LambdaUtils.extract(function1);
		// System.out.println(extract1.getImplMethodName());
		//
		// List<String> list = new ArrayList<>();
		// list.add("1");
		// System.out.println(list.toString());
		//
		// String[] strings = new String[] { "1" };
		// System.out.println(ExtStringUtil.isEmpty(" "));
		RestTemplate restTemplate = new RestTemplate();
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setConnectTimeout(120000);// 连接主机的超时时间（单位：毫秒）
		factory.setReadTimeout(120000);// 从主机读取数据的超时时间（单位：毫秒）
		restTemplate.setRequestFactory(factory);

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		// 处理请求远程服务报403 add by zhuyue 2023-04-06
		headers.add(HttpHeaders.USER_AGENT,
				"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
		HttpEntity<String> entity = new HttpEntity<String>("", headers);

		ResponseEntity<String> resp = restTemplate.postForEntity("http://127.0.0.1:9901/v1/admin/sys/sysmenu/getuserroleidlistbyroleids",
				entity, String.class);
		if (HttpStatus.OK == resp.getStatusCode()) {
			System.out.println("rest请求成功，返回报文：" + resp.getBody());

		} else {
			System.out.println("rest请求失败，返回报文：" + resp.getBody());
		}

	}

	public static <T, U> Function<T, U> propertyFunction(Class<T> clazz, String propertyName) {
		try {
			Method getterMethod = clazz.getMethod("get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
			@SuppressWarnings("unchecked")
			Function<T, U> function = (Function<T, U>) input -> {
				return (U) getterMethod.getAnnotatedExceptionTypes();
			};
			return function;
		} catch (Exception e) {
			throw new RuntimeException("Error creating property function", e);
		}
	}

	public static <T, U> SFunction<T, U> propertySFunction(Class<T> clazz, String propertyName) {
		try {
			Method getterMethod = clazz.getMethod("get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
			@SuppressWarnings("unchecked")
			SFunction<T, U> function = (SFunction<T, U>) input -> {
				try {
					return (U) getterMethod.invoke(input);
				} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
					e.printStackTrace();
				}
				return null;
			};
			return function;
		} catch (Exception e) {
			throw new RuntimeException("Error creating property function", e);
		}
	}

}
